/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.railway.app;

import com.railway.utils.CustomTableModel;
import com.railway.utils.CustomScrollBarUI;
import com.railway.utils.RestServer;
import com.railway.utils.TextFieldLimiter;
import com.railway.utils.Toast;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.text.AbstractDocument;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 *
 * @author Kenny
 */
public class TrainPanel extends javax.swing.JPanel {

    Image imgArrow = new ImageIcon(this.getClass().getResource("/com/railway/image/arrow.png")).getImage();
    private CardLayout cl;
    private ArrayList<String> colName;
    private ArrayList<ArrayList<Object>> rowData;
    private CustomTableModel ctmModel;
    private JSONObject json;
    private JSONArray jsonArray;
    private String status = "ACV";
    private String trainid;

    /**
     * Creates new form TrainPanel
     */
    public TrainPanel() {
        initComponents();
        cl = (CardLayout) jpTrainContent.getLayout();
        populateTableData();
    }

    private void populateTableData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                colName = new ArrayList<>();
                colName.add("Train ID");
                colName.add("Train Name");
                colName.add("Status");
                String result = RestServer.httpGet("train", "");
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            rowData = new ArrayList<>();
                            jsonArray = json.getJSONArray("content");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                ArrayList<Object> temp = new ArrayList<>();
                                temp.add(jsonArray.getJSONObject(i).get("trainId"));
                                temp.add(jsonArray.getJSONObject(i).get("trainName"));
                                String tempStatus = null;
                                switch(jsonArray.getJSONObject(i).getString("status")){
                                    case "ACV":
                                        tempStatus = "Active";
                                        break;
                                    case "INV":
                                        tempStatus = "Inactive";
                                        break;
                                    case "MNT":
                                        tempStatus = "Maintenance";
                                        break;
                                }
                                temp.add(tempStatus);
                                rowData.add(temp);
                            }
                            ctmModel = new CustomTableModel(colName, rowData);
                            jtbTrains.setModel(ctmModel);
                            jtbTrains.removeColumn(jtbTrains.getColumnModel().getColumn(0));
                        } else {
                            Toast.makeText(getParent(), "No train details found in database.", Toast.LENGTH_SHORT).display();
                        }
                    } else {
                        Toast.makeText(getParent(), json.getString("message"), Toast.LENGTH_SHORT).display();
                    }
                } catch (JSONException ex) {
                    Logger.getLogger(TrainPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }).start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbgAStatus = new javax.swing.ButtonGroup();
        jbgMStatus = new javax.swing.ButtonGroup();
        jpAddArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpTrain = new javax.swing.JPanel();
        jlblTrains = new javax.swing.JLabel();
        jspTrainsScroller = new javax.swing.JScrollPane();
        jtbTrains = new javax.swing.JTable();
        jsprTrains = new javax.swing.JSeparator();
        jPanel1 = new javax.swing.JPanel();
        jbtRemoveTrain = new javax.swing.JButton();
        jbtModifyTrain = new javax.swing.JButton();
        jbtAddTrain = new javax.swing.JButton();
        jpTrainContent = new javax.swing.JPanel();
        jpNullCard = new javax.swing.JPanel();
        jpATrainCard = new javax.swing.JPanel();
        jpAddTrainArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };
        jpAddTrain = new javax.swing.JPanel();
        jlblATrainName = new javax.swing.JLabel();
        jtfATrainName = new javax.swing.JTextField();
        jlblAStatus = new javax.swing.JLabel();
        jrbAActive = new javax.swing.JRadioButton();
        jrbAInactive = new javax.swing.JRadioButton();
        jrbAMaintenance = new javax.swing.JRadioButton();
        jbtAReset = new javax.swing.JButton();
        jbtASubmit = new javax.swing.JButton();
        jpMTrainCard = new javax.swing.JPanel();
        jpModifyTrain = new javax.swing.JPanel();
        jtfMTrainName = new javax.swing.JTextField();
        jlblMTrainName = new javax.swing.JLabel();
        jlblMStatus = new javax.swing.JLabel();
        jrbMActive = new javax.swing.JRadioButton();
        jrbMInactive = new javax.swing.JRadioButton();
        jrbMMaintenance = new javax.swing.JRadioButton();
        jbtMSubmit = new javax.swing.JButton();
        jpModifyTrainArrow = new javax.swing.JPanel(){
            protected void paintComponent(Graphics g){
                super.paintComponent(g);
                g.drawImage(imgArrow, 0, 0, getWidth(), imgArrow.getHeight(this), this);
            }
        };

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(860, 375));
        setLayout(null);

        jpAddArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpAddArrowLayout = new javax.swing.GroupLayout(jpAddArrow);
        jpAddArrow.setLayout(jpAddArrowLayout);
        jpAddArrowLayout.setHorizontalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 180, Short.MAX_VALUE)
        );
        jpAddArrowLayout.setVerticalGroup(
            jpAddArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        add(jpAddArrow);
        jpAddArrow.setBounds(0, 0, 180, 10);

        jpTrain.setBackground(new java.awt.Color(255, 255, 255));
        jpTrain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpTrain.setLayout(null);

        jlblTrains.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jlblTrains.setText("Trains");
        jpTrain.add(jlblTrains);
        jlblTrains.setBounds(545, 0, 300, 20);

        jspTrainsScroller.getVerticalScrollBar().setUI(new CustomScrollBarUI());

        jtbTrains.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jspTrainsScroller.setViewportView(jtbTrains);

        jpTrain.add(jspTrainsScroller);
        jspTrainsScroller.setBounds(545, 25, 300, 330);
        jpTrain.add(jsprTrains);
        jsprTrains.setBounds(545, 20, 300, 5);

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jPanel1.setLayout(null);

        jbtRemoveTrain.setBackground(new java.awt.Color(155, 89, 182));
        jbtRemoveTrain.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtRemoveTrain.setForeground(new java.awt.Color(255, 255, 255));
        jbtRemoveTrain.setText("Deactivate Train");
        jbtRemoveTrain.setFocusPainted(false);
        jbtRemoveTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtRemoveTrainActionPerformed(evt);
            }
        });
        jPanel1.add(jbtRemoveTrain);
        jbtRemoveTrain.setBounds(385, 5, 140, 50);

        jbtModifyTrain.setBackground(new java.awt.Color(155, 89, 182));
        jbtModifyTrain.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtModifyTrain.setForeground(new java.awt.Color(255, 255, 255));
        jbtModifyTrain.setText("Modify Train");
        jbtModifyTrain.setFocusPainted(false);
        jbtModifyTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtModifyTrainActionPerformed(evt);
            }
        });
        jPanel1.add(jbtModifyTrain);
        jbtModifyTrain.setBounds(195, 5, 140, 50);

        jbtAddTrain.setBackground(new java.awt.Color(155, 89, 182));
        jbtAddTrain.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAddTrain.setForeground(new java.awt.Color(255, 255, 255));
        jbtAddTrain.setText("Add Train");
        jbtAddTrain.setFocusPainted(false);
        jbtAddTrain.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAddTrainActionPerformed(evt);
            }
        });
        jPanel1.add(jbtAddTrain);
        jbtAddTrain.setBounds(5, 5, 140, 50);

        jpTrainContent.setBackground(new java.awt.Color(255, 255, 255));
        jpTrainContent.setLayout(new java.awt.CardLayout());

        jpNullCard.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpNullCardLayout = new javax.swing.GroupLayout(jpNullCard);
        jpNullCard.setLayout(jpNullCardLayout);
        jpNullCardLayout.setHorizontalGroup(
            jpNullCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 520, Short.MAX_VALUE)
        );
        jpNullCardLayout.setVerticalGroup(
            jpNullCardLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 290, Short.MAX_VALUE)
        );

        jpTrainContent.add(jpNullCard, "nullcard");

        jpATrainCard.setBackground(new java.awt.Color(255, 255, 255));
        jpATrainCard.setLayout(null);

        jpAddTrainArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpAddTrainArrowLayout = new javax.swing.GroupLayout(jpAddTrainArrow);
        jpAddTrainArrow.setLayout(jpAddTrainArrowLayout);
        jpAddTrainArrowLayout.setHorizontalGroup(
            jpAddTrainArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );
        jpAddTrainArrowLayout.setVerticalGroup(
            jpAddTrainArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpATrainCard.add(jpAddTrainArrow);
        jpAddTrainArrow.setBounds(0, 0, 140, 10);

        jpAddTrain.setBackground(new java.awt.Color(255, 255, 255));
        jpAddTrain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpAddTrain.setLayout(null);

        jlblATrainName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblATrainName.setText("Train Name");
        jpAddTrain.add(jlblATrainName);
        jlblATrainName.setBounds(20, 30, 100, 30);

        AbstractDocument aDoc = (AbstractDocument)jtfATrainName.getDocument();
        aDoc.setDocumentFilter(new TextFieldLimiter("\\d{0,4}"));
        jtfATrainName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpAddTrain.add(jtfATrainName);
        jtfATrainName.setBounds(120, 30, 290, 30);

        jlblAStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblAStatus.setText("Status");
        jpAddTrain.add(jlblAStatus);
        jlblAStatus.setBounds(20, 90, 100, 30);

        jrbAActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAStatus.add(jrbAActive);
        jrbAActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbAActive.setSelected(true);
        jrbAActive.setText("Active");
        jrbAActive.setActionCommand("ACV");
        jrbAActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbAActiveActionPerformed(evt);
            }
        });
        jpAddTrain.add(jrbAActive);
        jrbAActive.setBounds(120, 90, 90, 30);

        jrbAInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgAStatus.add(jrbAInactive);
        jrbAInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbAInactive.setText("Inactive");
        jrbAInactive.setActionCommand("INV");
        jrbAInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbAInactiveActionPerformed(evt);
            }
        });
        jpAddTrain.add(jrbAInactive);
        jrbAInactive.setBounds(210, 90, 90, 30);

        jrbAMaintenance.setBackground(new java.awt.Color(255, 255, 255));
        jbgAStatus.add(jrbAMaintenance);
        jrbAMaintenance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbAMaintenance.setText("Maintenance");
        jrbAMaintenance.setActionCommand("MNT");
        jrbAMaintenance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbAMaintenanceActionPerformed(evt);
            }
        });
        jpAddTrain.add(jrbAMaintenance);
        jrbAMaintenance.setBounds(300, 90, 110, 30);

        jbtAReset.setBackground(new java.awt.Color(39, 174, 96));
        jbtAReset.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtAReset.setForeground(new java.awt.Color(255, 255, 255));
        jbtAReset.setText("Reset");
        jbtAReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAResetActionPerformed(evt);
            }
        });
        jpAddTrain.add(jbtAReset);
        jbtAReset.setBounds(20, 200, 150, 50);

        jbtASubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtASubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtASubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtASubmit.setText("Submit");
        jbtASubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtASubmitActionPerformed(evt);
            }
        });
        jpAddTrain.add(jbtASubmit);
        jbtASubmit.setBounds(270, 200, 150, 50);

        jpATrainCard.add(jpAddTrain);
        jpAddTrain.setBounds(0, 10, 525, 285);

        jpTrainContent.add(jpATrainCard, "addcard");

        jpMTrainCard.setBackground(new java.awt.Color(255, 255, 255));
        jpMTrainCard.setLayout(null);

        jpModifyTrain.setBackground(new java.awt.Color(255, 255, 255));
        jpModifyTrain.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(220, 220, 220), 2));
        jpModifyTrain.setLayout(null);

        AbstractDocument mDoc = (AbstractDocument)jtfMTrainName.getDocument();
        mDoc.setDocumentFilter(new TextFieldLimiter("\\d{0,4}"));
        jtfMTrainName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jpModifyTrain.add(jtfMTrainName);
        jtfMTrainName.setBounds(120, 30, 290, 30);

        jlblMTrainName.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMTrainName.setText("Train Name");
        jpModifyTrain.add(jlblMTrainName);
        jlblMTrainName.setBounds(20, 30, 100, 30);

        jlblMStatus.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jlblMStatus.setText("Status");
        jpModifyTrain.add(jlblMStatus);
        jlblMStatus.setBounds(20, 90, 100, 30);

        jrbMActive.setBackground(new java.awt.Color(255, 255, 255));
        jbgMStatus.add(jrbMActive);
        jrbMActive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMActive.setSelected(true);
        jrbMActive.setText("Active");
        jrbMActive.setActionCommand("ACV");
        jrbMActive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMActiveActionPerformed(evt);
            }
        });
        jpModifyTrain.add(jrbMActive);
        jrbMActive.setBounds(120, 90, 90, 30);

        jrbMInactive.setBackground(new java.awt.Color(255, 255, 255));
        jbgMStatus.add(jrbMInactive);
        jrbMInactive.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMInactive.setText("Inactive");
        jrbMInactive.setActionCommand("INV");
        jrbMInactive.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMInactiveActionPerformed(evt);
            }
        });
        jpModifyTrain.add(jrbMInactive);
        jrbMInactive.setBounds(210, 90, 90, 30);

        jrbMMaintenance.setBackground(new java.awt.Color(255, 255, 255));
        jbgMStatus.add(jrbMMaintenance);
        jrbMMaintenance.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        jrbMMaintenance.setText("Maintenance");
        jrbMMaintenance.setActionCommand("MNT");
        jrbMMaintenance.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jrbMMaintenanceActionPerformed(evt);
            }
        });
        jpModifyTrain.add(jrbMMaintenance);
        jrbMMaintenance.setBounds(300, 90, 110, 30);

        jbtMSubmit.setBackground(new java.awt.Color(39, 174, 96));
        jbtMSubmit.setFont(new java.awt.Font("Segoe UI Emoji", 0, 14)); // NOI18N
        jbtMSubmit.setForeground(new java.awt.Color(255, 255, 255));
        jbtMSubmit.setText("Submit");
        jbtMSubmit.setFocusPainted(false);
        jbtMSubmit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtMSubmitActionPerformed(evt);
            }
        });
        jpModifyTrain.add(jbtMSubmit);
        jbtMSubmit.setBounds(270, 200, 150, 50);

        jpMTrainCard.add(jpModifyTrain);
        jpModifyTrain.setBounds(0, 10, 520, 280);

        jpModifyTrainArrow.setBackground(new java.awt.Color(255, 255, 255));

        javax.swing.GroupLayout jpModifyTrainArrowLayout = new javax.swing.GroupLayout(jpModifyTrainArrow);
        jpModifyTrainArrow.setLayout(jpModifyTrainArrowLayout);
        jpModifyTrainArrowLayout.setHorizontalGroup(
            jpModifyTrainArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 140, Short.MAX_VALUE)
        );
        jpModifyTrainArrowLayout.setVerticalGroup(
            jpModifyTrainArrowLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 10, Short.MAX_VALUE)
        );

        jpMTrainCard.add(jpModifyTrainArrow);
        jpModifyTrainArrow.setBounds(190, 0, 140, 10);

        jpTrainContent.add(jpMTrainCard, "modifycard");

        jPanel1.add(jpTrainContent);
        jpTrainContent.setBounds(5, 55, 520, 290);

        jpTrain.add(jPanel1);
        jPanel1.setBounds(5, 5, 530, 350);

        add(jpTrain);
        jpTrain.setBounds(5, 10, 850, 360);
    }// </editor-fold>//GEN-END:initComponents

    private void jbtAddTrainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAddTrainActionPerformed
        jbtAddTrain.setBackground(new Color(142, 68, 173));
        jbtModifyTrain.setBackground(new Color(155, 89, 182));
        jbtRemoveTrain.setBackground(new Color(155, 89, 182));
        jtbTrains.clearSelection();
        cl.show(jpTrainContent, "addcard");
    }//GEN-LAST:event_jbtAddTrainActionPerformed

    private void jbtModifyTrainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtModifyTrainActionPerformed
        jbtAddTrain.setBackground(new Color(155, 89, 182));
        jbtModifyTrain.setBackground(new Color(142, 68, 173));
        jbtRemoveTrain.setBackground(new Color(155, 89, 182));
        if(jtbTrains.getSelectedRow() >= 0){
            cl.show(jpTrainContent, "modifycard");
            jtfMTrainName.setText(jtbTrains.getValueAt(jtbTrains.getSelectedRow(), 0).toString());
            if(jtbTrains.getValueAt(jtbTrains.getSelectedRow(), 1).equals("Active")){
                jrbMActive.setSelected(true);
            }else if(jtbTrains.getValueAt(jtbTrains.getSelectedRow(), 1).equals("Inactive")){
                jrbMInactive.setSelected(true);
            }else{
                jrbMMaintenance.setSelected(true);
            }
            trainid = jtbTrains.getModel().getValueAt(jtbTrains.getSelectedRow(), 0).toString();
        }else{
            Toast.makeText(this, "Please make selection from table first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtModifyTrainActionPerformed

    private void jbtRemoveTrainActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtRemoveTrainActionPerformed
        jbtAddTrain.setBackground(new Color(155, 89, 182));
        jbtModifyTrain.setBackground(new Color(155, 89, 182));
        jbtRemoveTrain.setBackground(new Color(142, 68, 173));
        cl.show(jpTrainContent, "nullcard");
        if (jtbTrains.getSelectedRow() >= 0) {
            int option = JOptionPane.showConfirmDialog(this, "Are you sure you want to remove this train?", "Confirmation", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
            if (option == JOptionPane.YES_OPTION) {
                String result = RestServer.httpDelete("train", jtbTrains.getModel().getValueAt(jtbTrains.getSelectedRow(), 0).toString());
                try {
                    json = new JSONObject(result);
                    boolean error = json.optBoolean("error");
                    if (!error) {
                        int statuscode = json.getInt("status");
                        if (statuscode == 200) {
                            Toast.makeText(this, "Train successfully deactivated.", Toast.LENGTH_SHORT).display();
                            populateTableData();
                        } else {
                            Toast.makeText(this, "Unable to deactivate train now, please try again later.", Toast.LENGTH_SHORT).display();
                        }
                    } else {
                        Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
                    }
                } catch (JSONException ex) {
                    Logger.getLogger(TrainPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        } else {
            Toast.makeText(this, "Please make selection on table first.", Toast.LENGTH_SHORT).display();
        }
    }//GEN-LAST:event_jbtRemoveTrainActionPerformed

    private void jrbAActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbAActiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbAActiveActionPerformed

    private void jrbAInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbAInactiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbAInactiveActionPerformed

    private void jrbAMaintenanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbAMaintenanceActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbAMaintenanceActionPerformed

    private void jbtAResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAResetActionPerformed
        jtfATrainName.setText(null);
        jrbAActive.setSelected(true);
    }//GEN-LAST:event_jbtAResetActionPerformed

    private void jbtASubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtASubmitActionPerformed
        Map<String, Object> data = new HashMap<>();
        data.put("tname", jtfATrainName.getText());
        data.put("status", status);
        String result = RestServer.httpPost("train", data);
        try {
            json = new JSONObject(result);
            boolean error = json.optBoolean("error");
            if (!error) {
                int statuscode = json.getInt("status");
                if (statuscode == 200) {
                    Toast.makeText(this, "New train has been added.", Toast.LENGTH_SHORT).display();
                    populateTableData();
                } else {
                    Toast.makeText(this, "Unable to add train now, please try again later.", Toast.LENGTH_SHORT).display();
                }
            } else {
                Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
            }
        } catch (JSONException ex) {
            Logger.getLogger(TrainPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtASubmitActionPerformed

    private void jrbMActiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMActiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbMActiveActionPerformed

    private void jrbMInactiveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMInactiveActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbMInactiveActionPerformed

    private void jrbMMaintenanceActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jrbMMaintenanceActionPerformed
        status = evt.getActionCommand();
    }//GEN-LAST:event_jrbMMaintenanceActionPerformed

    private void jbtMSubmitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtMSubmitActionPerformed
        Map<String, Object> data = new HashMap<>();
        data.put("tname", jtfMTrainName.getText());
        data.put("status", status);
        String result = RestServer.httpPut("train", data, trainid);
        try{
            json = new JSONObject(result);
            boolean error = json.optBoolean("error");
            if(!error){
                int statuscode = json.getInt("status");
                if(statuscode == 200){
                    Toast.makeText(this, "Train details has been modified", Toast.LENGTH_SHORT).display();
                    populateTableData();
                } else {
                    Toast.makeText(this, "Unable to modify train now, please try again later.", Toast.LENGTH_SHORT).display();
                }
            } else {
                Toast.makeText(this, json.getString("message"), Toast.LENGTH_SHORT).display();
            }
        } catch (JSONException ex) {
            Logger.getLogger(TrainPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_jbtMSubmitActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanel1;
    private javax.swing.ButtonGroup jbgAStatus;
    private javax.swing.ButtonGroup jbgMStatus;
    private javax.swing.JButton jbtAReset;
    private javax.swing.JButton jbtASubmit;
    private javax.swing.JButton jbtAddTrain;
    private javax.swing.JButton jbtMSubmit;
    private javax.swing.JButton jbtModifyTrain;
    private javax.swing.JButton jbtRemoveTrain;
    private javax.swing.JLabel jlblAStatus;
    private javax.swing.JLabel jlblATrainName;
    private javax.swing.JLabel jlblMStatus;
    private javax.swing.JLabel jlblMTrainName;
    private javax.swing.JLabel jlblTrains;
    private javax.swing.JPanel jpATrainCard;
    private javax.swing.JPanel jpAddArrow;
    private javax.swing.JPanel jpAddTrain;
    private javax.swing.JPanel jpAddTrainArrow;
    private javax.swing.JPanel jpMTrainCard;
    private javax.swing.JPanel jpModifyTrain;
    private javax.swing.JPanel jpModifyTrainArrow;
    private javax.swing.JPanel jpNullCard;
    private javax.swing.JPanel jpTrain;
    private javax.swing.JPanel jpTrainContent;
    private javax.swing.JRadioButton jrbAActive;
    private javax.swing.JRadioButton jrbAInactive;
    private javax.swing.JRadioButton jrbAMaintenance;
    private javax.swing.JRadioButton jrbMActive;
    private javax.swing.JRadioButton jrbMInactive;
    private javax.swing.JRadioButton jrbMMaintenance;
    private javax.swing.JScrollPane jspTrainsScroller;
    private javax.swing.JSeparator jsprTrains;
    private javax.swing.JTable jtbTrains;
    private javax.swing.JTextField jtfATrainName;
    private javax.swing.JTextField jtfMTrainName;
    // End of variables declaration//GEN-END:variables
}
